﻿using BaseAuthRole.AspNetCore;
using FreeSql.Internal.Model;
using log4net;
using Microsoft.AspNetCore.Authorization.Infrastructure;
using Newtonsoft.Json;
using RS.BaseModel;
using RS.BaseModel.Common;
using System.Collections.Generic;
using TPWorkPlat.DTO.Input;
using TPWorkPlat.DTO.Output;
using TPWorkPlat.Entity;
using TPWorkPlat.IService;

namespace TPWorkPlat.Service
{
    public class TheoryStudyService : ITheoryStudyService
    {
        private IFreeSql _IfreeSql;

        private ILog logger = log4net.LogManager.GetLogger("TheoryStudyService");

        private readonly IBaseAuthClient _baseAuthClient;

        public TheoryStudyService(IFreeSql freeSql, IBaseAuthClient baseAuthClient) {

            _IfreeSql = freeSql;
            _baseAuthClient= baseAuthClient;
        }
        /// <summary>
        /// 获取首页学习动态列表
        /// </summary>
        /// <returns></returns>
        public CommonResult<IList<TheoryStudyNewsOutput>> GetHomeList(TheoryStudyHomeInput theoryStudyHomeInput)
        {

            var result = new CommonResult<IList<TheoryStudyNewsOutput>>();

            var info = this._IfreeSql.Select<TheoryStudyNewsEntity>().OrderByPropertyName(theoryStudyHomeInput.orderbyCol, false).Limit(theoryStudyHomeInput.pageSize).ToList<TheoryStudyNewsOutput>();

            result.IsError = false;

            result.Result = info;

            return result;

        }
        /// <summary>
        /// 获取理论动态分页列表
        /// </summary>
        /// <param name="theoryStudyGetListAndPageInfoInput"></param>
        /// <returns></returns>
        public CommonResult<ListDataOutput<T>> GetNewsListAndPageInfo<T>(TheoryStudyGetListAndPageInfoInput theoryStudyGetListAndPageInfoInput)
        {
            var result = new CommonResult<ListDataOutput<T>>();
            var info = this._IfreeSql.Select<TheoryStudyNewsEntity>().WhereDynamicFilter(theoryStudyGetListAndPageInfoInput.dfi).Count(out var totalCnt).
                                           Page(theoryStudyGetListAndPageInfoInput.pageIndex, theoryStudyGetListAndPageInfoInput.pageSize).ToList<T>();
            var DataResult = new ListDataOutput<T>();
            DataResult.DataLst = info;
            DataResult.PageSize = theoryStudyGetListAndPageInfoInput.pageSize;
            DataResult.PageIndex = theoryStudyGetListAndPageInfoInput.pageIndex;
            DataResult.CountAll = totalCnt;
            result.IsError = false;

            result.Result = DataResult;

            return result;

        }
        /// <summary>
        /// 获取理论动态详情
        /// </summary>
        /// <param name="baim">传入Id即可</param>
        /// <returns></returns>
        public CommonResult<TheoryStudyNewsEntity> GetTheoryStudyNewsFromId(BaseAppAndUserInputModel bauim)
        {
            var result = new CommonResult<TheoryStudyNewsEntity>();
            var info = this._IfreeSql.Select<TheoryStudyNewsEntity>().Where(w=>w.Id== bauim.Id).First();
            result.IsError = false;

            result.Result = info;

            return result;

        }
        /// <summary>
        /// 获取一学一报分页列表
        /// </summary>
        /// <param name="theoryStudyGetListAndPageInfoInput"></param>
        /// <returns></returns>
        public CommonResult<ListDataOutput<T>> GetFillingListAndPageInfo<T>(TheoryStudyGetListAndPageInfoInput theoryStudyGetListAndPageInfoInput)
        {
            var result = new CommonResult<ListDataOutput<T>>();
            var info = this._IfreeSql.Select<TheoryStudyFillingEntity>().WhereDynamicFilter(theoryStudyGetListAndPageInfoInput.dfi).Count(out var totalCnt).
                                           Page(theoryStudyGetListAndPageInfoInput.pageIndex, theoryStudyGetListAndPageInfoInput.pageSize).ToList<T>();
            var DataResult = new ListDataOutput<T>();
            DataResult.DataLst = info;
            DataResult.PageSize = theoryStudyGetListAndPageInfoInput.pageSize;
            DataResult.PageIndex = theoryStudyGetListAndPageInfoInput.pageIndex;
            DataResult.CountAll = totalCnt;
            result.IsError = false;
            result.Result = DataResult;
            return result;
        }
        /// <summary>
        /// 获取一学一报详情
        /// </summary>
        /// <param name="baim">传入Id即可</param>
        /// <returns></returns>
        public CommonResult<TheoryStudyFillingEntity> GetTheoryStudyFillingFromId(BaseAppAndUserInputModel bauim)
        {
            var result = new CommonResult<TheoryStudyFillingEntity>();
            var info = this._IfreeSql.Select<TheoryStudyFillingEntity>().Where(w => w.Id == bauim.Id).First();
            result.IsError = false;
            result.Result = info;
            return result;
        }
        public CommonResult<IList<TheoryStudyFillingDateOutput>> GetFillingDateList(TheoryStudyGetDateListInput theoryStudyGetDateListInput)
        {
            var result = new CommonResult<IList<TheoryStudyFillingDateOutput>>();
            string? dStartDate = theoryStudyGetDateListInput.startDate;
            string? dEndDate = theoryStudyGetDateListInput.endDate;
            DateTime now = DateTime.Now;
            if (string.IsNullOrEmpty(theoryStudyGetDateListInput.startDate))
            {
                dStartDate = new DateTime(now.Year, now.Month, 1).ToString("yyyy-MM-dd HH:mm:ss");
            }
            if (string.IsNullOrEmpty(theoryStudyGetDateListInput.endDate))
            {
                dEndDate = now.AddDays(1 - now.Day).AddMonths(1).AddDays(-1).AddHours(23 - now.Hour).ToString("yyyy-MM-dd HH:mm:ss");
            }
            var info = this._IfreeSql.Select<TheoryStudyFillingEntity>().Where(w => w.StudyDate >= Convert.ToDateTime(dStartDate) && w.StudyDate <= Convert.ToDateTime(dEndDate))
                                                .GroupBy(g => g.StudyDate.Date).ToList(a => new TheoryStudyFillingDateOutput{ StudyDate = a.Key, StudyCnt = a.Count() });
            result.IsError = false;
            result.Result = info;
            return result;
        }
        /// <summary>
        /// 获取学习提醒分页列表
        /// </summary>
        /// <param name="theoryStudyGetListAndPageInfoInput"></param>
        /// <returns></returns>
        public CommonResult<ListDataOutput<T>> GetPlanListAndPageInfo<T>(TheoryStudyGetListAndPageInfoInput theoryStudyGetListAndPageInfoInput)
        {
            var result = new CommonResult<ListDataOutput<T>>();
            var info = this._IfreeSql.Select<TheoryStudyPlanEntity>().WhereDynamicFilter(theoryStudyGetListAndPageInfoInput.dfi).Count(out var totalCnt).
                                           Page(theoryStudyGetListAndPageInfoInput.pageIndex, theoryStudyGetListAndPageInfoInput.pageSize).ToList<T>();
            var DataResult = new ListDataOutput<T>();
            DataResult.DataLst = info;
            DataResult.PageSize = theoryStudyGetListAndPageInfoInput.pageSize;
            DataResult.PageIndex = theoryStudyGetListAndPageInfoInput.pageIndex;
            DataResult.CountAll = totalCnt;
            result.IsError = false;
            result.Result = DataResult;
            return result;
        }
        /// <summary>
        /// 获取学习提醒日期列表
        /// </summary>
        /// <param name="theoryStudyGetListAndPageInfoInput"></param>
        /// <returns></returns>
        public CommonResult<ListDataOutput<T>> GetPlanDateListAndPageInfo<T>(TheoryStudyGetListAndPageInfoInput theoryStudyGetListAndPageInfoInput)
        {
            var result = new CommonResult<ListDataOutput<T>>();
            var info = this._IfreeSql.Select<TheoryStudyPlanDateEntity>().WhereDynamicFilter(theoryStudyGetListAndPageInfoInput.dfi).Count(out var totalCnt).
                                           Page(theoryStudyGetListAndPageInfoInput.pageIndex, theoryStudyGetListAndPageInfoInput.pageSize).ToList<T>();
            var DataResult = new ListDataOutput<T>();
            DataResult.DataLst = info;
            DataResult.PageSize = theoryStudyGetListAndPageInfoInput.pageSize;
            DataResult.PageIndex = theoryStudyGetListAndPageInfoInput.pageIndex;
            DataResult.CountAll = totalCnt;
            result.IsError = false;
            result.Result = DataResult;
            return result;
        }
        /// <summary>
        /// 获取学习提醒详情【包含提醒日期列表数据】
        /// </summary>
        /// <param name="baim">传入Id即可</param>
        /// <returns></returns>
        public CommonResult<TheoryStudyPlanDetailOutput> GetTheoryStudyPlanFromId(BaseAppAndUserInputModel bauim)
        {
            var result = new CommonResult<TheoryStudyPlanDetailOutput>();
            var info = this._IfreeSql.Select<TheoryStudyPlanEntity>().Where(w => w.Id == bauim.Id).ToList<TheoryStudyPlanDetailOutput>();
            if (info.Count > 0)
            {
                result.Result = info[0];
                //处理提醒日期
                var infoDateLst = this._IfreeSql.Select<TheoryStudyPlanDateEntity>().Where(w => w.StudyPlanUniqueId == result.Result.UniqueId).ToList<TheoryStudyPlanDateOutput>();
                result.Result.StudyPlanDateLst = infoDateLst;
            }
            result.IsError = false;
            return result;
        }
        /// <summary>
        /// 获取专家信息分页列表
        /// </summary>
        /// <param name="theoryStudyGetListAndPageInfoInput"></param>
        /// <returns></returns>
        public CommonResult<ListDataOutput<T>> GetExpertInfoListAndPageInfo<T>(TheoryStudyGetListAndPageInfoInput theoryStudyGetListAndPageInfoInput)
        {
            var result = new CommonResult<ListDataOutput<T>>();
            var info = this._IfreeSql.Select<ExpertInfoEntity>().WhereDynamicFilter(theoryStudyGetListAndPageInfoInput.dfi).Count(out var totalCnt).
                                           Page(theoryStudyGetListAndPageInfoInput.pageIndex, theoryStudyGetListAndPageInfoInput.pageSize).ToList<T>();
            var DataResult = new ListDataOutput<T>();
            DataResult.DataLst = info;
            DataResult.PageSize = theoryStudyGetListAndPageInfoInput.pageSize;
            DataResult.PageIndex = theoryStudyGetListAndPageInfoInput.pageIndex;
            DataResult.CountAll = totalCnt;
            result.IsError = false;
            result.Result = DataResult;
            return result;
        }
        /// <summary>
        /// 获取专家信息详情
        /// </summary>
        /// <param name="baim">传入Id即可</param>
        /// <returns></returns>
        public CommonResult<ExpertInfoEntity> GetExpertInfoFromId(BaseAppAndUserInputModel bauim)
        {
            var result = new CommonResult<ExpertInfoEntity>();
            var info = this._IfreeSql.Select<ExpertInfoEntity>().Where(w => w.Id == bauim.Id).First();
            result.IsError = false;
            result.Result = info;
            return result;
        }
        /// <summary>
        /// 获取专家成果分页列表
        /// </summary>
        /// <param name="theoryStudyGetListAndPageInfoInput"></param>
        /// <returns></returns>
        public CommonResult<ListDataOutput<T>> GetExpertAchievementsListAndPageInfo<T>(TheoryStudyGetListAndPageInfoInput theoryStudyGetListAndPageInfoInput)
        {
            var result = new CommonResult<ListDataOutput<T>>();
            var info = this._IfreeSql.Select<ExpertAchievementsEntity>().WhereDynamicFilter(theoryStudyGetListAndPageInfoInput.dfi).Count(out var totalCnt).
                                           Page(theoryStudyGetListAndPageInfoInput.pageIndex, theoryStudyGetListAndPageInfoInput.pageSize).ToList<T>();
            var DataResult = new ListDataOutput<T>();
            DataResult.DataLst = info;
            DataResult.PageSize = theoryStudyGetListAndPageInfoInput.pageSize;
            DataResult.PageIndex = theoryStudyGetListAndPageInfoInput.pageIndex;
            DataResult.CountAll = totalCnt;
            result.IsError = false;
            result.Result = DataResult;
            return result;
        }
        /// <summary>
        /// 获取专家成果详情
        /// </summary>
        /// <param name="baim">传入Id即可</param>
        /// <returns></returns>
        public CommonResult<ExpertAchievementsEntity> GetExpertAchievementsFromId(BaseAppAndUserInputModel bauim)
        {
            var result = new CommonResult<ExpertAchievementsEntity>();
            var info = this._IfreeSql.Select<ExpertAchievementsEntity>().Where(w => w.Id == bauim.Id).First();
            result.IsError = false;
            result.Result = info;
            return result;
        }
    }
}